<?php
if (!defined('__COLA_DIR__')) exit;

/**
 * 标签管理列表组件
 *
 * @author edikud
 * @package Widget
 * @copyright Copyright (c) 2019 Cola (http://www.mcooo.com)
 * @license GNU General Public License 2.0
 */
class Widget_Metas_Tag_Ucenter extends Widget_Abstract_Metas
{
	
	
    /**
     * 内容个数
     *
     * @access private
     * @var integer
     */
    protected $_total = false;

    /**
     * 当前页
     *
     * @access private
     * @var integer
     */
    protected $_currentPage = 1;
	
    /**
     * 入口函数
     *
     * @access public
     * @return void
     */
    public function execute()
    {
		$this->user->pass('editor');

        $this->parameter->setDefault('pageSize=20');
        $this->_currentPage = $this->request->get('page', 1);
		
        /** 构建基础查询 */
        $select = $this->select();
		
		/** 按tag查询 */
        $select->where('table.metas.type = ?', 'tag');
		
        /** 过滤标题 */
        if (NULL != ($keywords = $this->request->filter('search')->keywords)) {
            $args = array();
            $keywordsList = explode(' ', $keywords);
            $args[] = implode(' OR ', array_fill(0, count($keywordsList), 'table.metas.name LIKE ?'));

            foreach ($keywordsList as $keyword) {
                $args[] = '%' . $keyword . '%';
            }

            call_user_func_array(array($select, 'where'), $args);
        }

		/** 计算数目 */
		$this->_total = (clone $select)
            ->select(array('COUNT(*)' => 'num'))
            ->fetchObject()->num;

        /** 提交查询 */
        $select->order('table.metas.mid', 'DESC')
        ->page($this->_currentPage, $this->parameter->pageSize);

        $select->fetchAll(array($this, 'push'));
    }

	
    /**
     * 输出分页
     *
     * @access public
     * @return void
     */
    public function pageNav()
    {
        $query = $this->request->makeUriByRequest('page={page}');

        /** 使用盒状分页 */
        $nav = new Page_Box($this->_total,
        $this->_currentPage, $this->parameter->pageSize, $query);
        return $nav->render('&laquo;', '&raquo;');
    }
	
    public function tagList()
    {
		
        $this->widget('Widget_Scripts')->set(
<<<EOF

		
      //pop('Howdy!').show(1).width(500).title('textnnnnnnnnnnn').content('gggg');
		
		//pop({buttons:'',resize: !0,}).show();
		
		//$.alert('message');$.alert('message2');$.alert('message3');
		
		//pop().content('gggg').close();
		
EOF
);
		
		
		
        $html = '<form method="get">
		<div class="cola-search">
		<input type="text" placeholder="'._t('请输入关键字').'" value="'. htmlspecialchars($this->request->keywords).'" name="keywords" /> ';
        if ('' != $this->request->keywords || '' != $this->request->category || '' != $this->request->uid || '' != $this->request->all){
            $html .= '<a class="cola-search-cancel ci-cancel" href="'.$this->options->ucenterUrl('tags', false).'"></a>';
	    }
        $html .= '</div></form>';
		
        if ($this->have()) {
            $WidgetMenu = $this->widget('Widget_Menu');
			$Layout = new Layout();
			$Layout->header(array(
				$this->widget('Widget_Metas_Tag_Edit')->operate(),
			    _t('名称'),
				_t('缩略名'),
			));
			while ($this->next()){
			    $Layout->lists(array(
				    '<a class="cola-balloon" href="'.$this->options->ucenterUrl('posts?category=' . $this->mid, false).'">'.$this->contentsNum.'</a>&nbsp;&nbsp;<a href="' .$this->options->ucenterUrl('tag?mid=' . $this->mid, false).'">'.$this->name.'</a>&nbsp;&nbsp;<a class="text-muted text-md" href="'.$this->permalink.'"><i class="ci-link"></i></a>',
					'<span class="text-muted text-md">' . $this->alias . '</span>'
				), array('data-mid'=>$this->mid));
			}
			$Layout->append('<ul class="cola-pager">' . $this->pageNav() . '</ul>');
			$html .= $Layout->render();
		}

		return $html;
	}
}
